package in.muton.edushare.components.tasks.electives;

import in.muton.edushare.components.tasks.electives.api.SerialChecker;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.coursepages.CoursePage;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.ElectivePreferenceDAO;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class CommonElectivePanel extends EdusharePanel {
	private static final long serialVersionUID = -5731758457032445768L;

	public CommonElectivePanel(String id, EduShareBasePage page, final IModel<Curricula> currModel,
			IModel<Users> userModel, final List<Integer> groupIDs,
			final int semester) {
		super(id, page);
		CurriculumDAO cDAO = new CurriculumDAO(getORMInterface());
		
		Form<Void> f;
		add(f = new Form<Void>("mainForm") {
			@Override
			public void onSubmit() {
				SerialChecker sc = new SerialChecker();
				sc.checkElectivePreference(map);
				if(sc.isError()){
					setError(sc.getErrorMessage());
					return;
				}else{
					setError("");
				}
					
				if (currModel.getObject().getUniversity()
						.getElectiveSelectionTime() != Universities.UNIV_ALLOW_ELECTIVE_SELECTION) {
					setError("Elective choice selection has been closed by the university administration.<br/>"
							+ ApplicationConstants.APPLICATION_NAME
							+ " will not be able to save your choices");
					return;
				}

				ElectivePreferenceDAO eDAO = new ElectivePreferenceDAO(
						getORMInterface());
				CurriculumDAO cDAO = new CurriculumDAO(getORMInterface());
				Set<Entry<IModel<Courses>, TextField<String>>> set = map
					.entrySet();
				for (Entry<IModel<Courses>, TextField<String>> s : set) {
					for (Integer i : groupIDs) {
						eDAO.addSetOfPreferences(getLoggedInUser(), cDAO
								.getSemCourse(currModel.getObject(), semester,
										i, s.getKey().getObject()), i, Integer
								.parseInt(s.getValue().getModelObject()));
					}
				}
			}

			private static final long serialVersionUID = 4159395869312591690L;
		});
		f.add(new Label("electiveGroup", CompletedCommonElectivePanel.getTitleString(groupIDs) + " of "
				+ currModel.getObject().getCurriculaName() + " for semester "
				+ (semester+1) + "."));

		int selectNum = groupIDs.size();
		int totalNum = cDAO.getElectiveCourses(currModel.getObject(), semester,
				groupIDs.get(0)).size();
		f.add(new Label("selectNum", "" + selectNum));
		f.add(new Label("totalNum", "" + totalNum));
		CayenneList<Courses> courseList = new CayenneList<Courses>(cDAO
				.getElectiveCourses(currModel.getObject(), semester, groupIDs
						.get(0)));

		f.add(new ListView<IModel<Courses>>("choiceListLV", courseList) {
			@Override
			protected void populateItem(final ListItem<IModel<Courses>> arg0) {
				Link<Void> l;
				arg0.add(new Label("serialNum", (arg0.getIndex() + 1) + ""));
				arg0.add(l = new BookmarkablePageLink<Void>("courseLink",
						CoursePage.class, EntityPageParams.getParams(arg0
								.getModelObject())));
				l.add(new Label("courseName", arg0.getModelObject().getObject()
						.getCourseName()));
				TextField<String> tf = new TextField<String>("preference",
						new Model<String>() {
							@Override
							public String getObject() {
								return s;
							}

							@Override
							public void setObject(String str) {
								s = str;
							}

							private String s = "" + (arg0.getIndex() + 1);
							private static final long serialVersionUID = 1L;
						});
				arg0.add(tf);
				map.put(arg0.getModelObject(), tf);
			}

			private static final long serialVersionUID = 6484186314418893973L;
		});
	}

	private HashMap<IModel<Courses>, TextField<String>> map = new HashMap<IModel<Courses>, TextField<String>>();
}
